//
//  UserAccount.swift
//  微博个人swift
//
//  Created by 李灏 on 16/2/17.
//  Copyright © 2016年 lh. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {


    //用于调用access_token，接口获取授权后的access token。
    var access_token:String?
    
    //access_token的生命周期，单位是秒数。
    var expires_in:NSTimeInterval = 0 {
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //token过期的日期 开发者账号的过期日期是 5年 普通用户的账户过期日期是3天
    var expires_date:NSDate?
    
    //友好显示名称
    var name:String?
    
    //用户头像地址（大图），180×180像素
    var avatar_large:String?
    
    //当前授权用户的UID。  标识一个唯一的用户
    var uid:String?
    
    //使用KVC赋值
    init(dict:[String:AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //过滤操作
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    //重写description属性
    override var description: String {
        let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        return dict.description
    }
    //MARK:储存模型数据
    func saveAccount(){
        //保存在沙盒中
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("useraccount.plist")
        print(path)
        //调用归档方法
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    //MARK:读取模型数据
    class func loadUserAccount() -> UserAccount? {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("useraccount.plist")
        if let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            //判断token是否过期  算出日期和当前日期比较
            //只有过期日期大于当前日期 才没有过期
            if userAccount.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount
            }
        }
    
        return nil
    }
    
    //实现归档解档方法
    //解档
    required init?(coder aDecoder: NSCoder) {
        //给当前对象属性赋值
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        //基本数据类型不用转换类型
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
    }

}
